var api_url = "";
var api_token = "";

//通过分类名称获取视频分类id
//不同的接口匹配不同的id
//避免出现id固定
function get_type_id(type_name){
	var type_id = null;
	$.ajax({
		type: 'GET',
		async: false, //修改此方法为同步，解决ajax是异步请求问题
		url: api_url+'?ac=list&token='+api_token,
		dataType: 'json',
		success(data) {
			types = data.class
			for(i=0;i<types.length;i++){
				types_name = data.class[i].type_name;
				if(types_name.indexOf(type_name)>-1){
					type_id = data.class[i].type_id;
					// console.log(data.class[i])
					break;
				}
			}
		},
		error(jqXHR, textStatus, errorThrown) {
			// 错误信息处理
			console.error(textStatus, errorThrown)
		}
	});
	return type_id;
}

//获取热播数据
function get_index() {
	$.ajax({
		type: 'GET',
		async: false, //修改此方法为同步，解决ajax是异步请求问题
		url: api_url+'?ac=list&token='+api_token,
		dataType: 'json',
		success(data) {
			reboData = data.list;
			// console.log(reboData[0])
			for(i=0;i<5;i++){
				//影视名称
				vod_name = reboData[i].vod_name;
				//影视更新集数
				vod_remarks = reboData[i].vod_remarks;
				//影视数据id
				vod_id = reboData[i].vod_id;
				//类别id
				type_id = reboData[i].type_id;
				//类别名称
				type_name = reboData[i].type_name;
				//获取剧集详细信息
				infoData = get_info(vod_id);
				//封面信息
				vod_pic = infoData.vod_pic;

				var div_str = '<div class="col-md-3">' +
					'<div class="thumbnail">' +
					'<img src="'+vod_pic+'" typeid="'+type_id+'" vodid="'+vod_id+'" onclick="get_info_jump(this)" alt="图片正在赶来的路上..."' +
					'title="'+vod_name+'"class="u3_div_img" ><div class="caption">' +
					'<a onclick="get_info_jump(this)" typeid="'+type_id+'" vodid="'+vod_id+'" class="u3_div_text" title="' + vod_name +'">' + vod_name + '</a>' +
					'<p class="u3_div_text_p">' + vod_remarks + '</p>' +
					'</div>' + '</div>' + '</div>'
				$("#u3_div_rebo").append(div_str);

				var dianying = get_Movs(7,1)
				//影视名称
				dianying_vod_name = dianying[i].vod_name;
				//影视更新集数
				dianying_vod_remarks = dianying[i].vod_remarks;
				//影视数据id
				dianying_vod_id = dianying[i].vod_id;
				//类别id
				dianying_type_id = dianying[i].type_id;
				//类别名称
				dianying_type_name = dianying[i].type_name;
				//获取剧集详细信息
				dianying_infoData = get_info(dianying_vod_id);
				//封面信息
				dianying_vod_pic = dianying_infoData.vod_pic;

				var div_str = '<div class="col-md-3">' +
					'<div class="thumbnail">' +
					'<img src="'+dianying_vod_pic+'" typeid="'+dianying_type_id+'" vodid="'+dianying_vod_id+'" onclick="get_info_jump(this)" alt="图片正在赶来的路上..."' +
					'title="'+dianying_vod_name+'"class="u3_div_img" ><div class="caption">' +
					'<a onclick="get_info_jump(this)" typeid="'+dianying_type_id+'" vodid="'+dianying_vod_id+'" class="u3_div_text" title="' + dianying_vod_name +'">' + dianying_vod_name + '</a>' +
					'<p class="u3_div_text_p">' + dianying_vod_remarks + '</p>' +
					'</div>' + '</div>' + '</div>'

				$("#u3_div_dy").append(div_str);

				var dianshiju = get_Movs(13,1)
				//影视名称
				dianshiju_vod_name = dianshiju[i].vod_name;
				//影视更新集数
				dianshiju_vod_remarks = dianshiju[i].vod_remarks;
				//影视数据id
				dianshiju_vod_id = dianshiju[i].vod_id;
				//类别id
				dianshiju_type_id = dianshiju[i].type_id;
				//类别名称
				dianshiju_type_name = dianshiju[i].type_name;
				//获取剧集详细信息
				dianshiju_infoData = get_info(dianshiju_vod_id);
				//封面信息
				dianshiju_vod_pic = dianshiju_infoData.vod_pic;

				var div_str = '<div class="col-md-3">' +
					'<div class="thumbnail">' +
					'<img src="'+dianshiju_vod_pic+'" typeid="'+dianshiju_type_id+'" vodid="'+dianshiju_vod_id+'" onclick="get_info_jump(this)" alt="图片正在赶来的路上..."' +
					'title="'+dianshiju_vod_name+'"class="u3_div_img" ><div class="caption">' +
					'<a onclick="get_info_jump(this)" typeid="'+dianshiju_type_id+'" vodid="'+dianshiju_vod_id+'" class="u3_div_text" title="' + dianshiju_vod_name +'">' + dianshiju_vod_name + '</a>' +
					'<p class="u3_div_text_p">' + dianshiju_vod_remarks + '</p>' +
					'</div>' + '</div>' + '</div>'
				$("#u3_div_dsj").append(div_str);

				var zongyi = get_Movs(3,1)
				//影视名称
				zongyi_vod_name = zongyi[i].vod_name;
				//影视更新集数
				zongyi_vod_remarks = zongyi[i].vod_remarks;
				//影视数据id
				zongyi_vod_id = zongyi[i].vod_id;
				//类别id
				zongyi_type_id = zongyi[i].type_id;
				//类别名称
				zongyi_type_name = zongyi[i].type_name;
				//获取剧集详细信息
				zongyi_infoData = get_info(zongyi_vod_id);
				//封面信息
				zongyi_vod_pic = zongyi_infoData.vod_pic;

				var div_str = '<div class="col-md-3">' +
					'<div class="thumbnail">' +
					'<img src="'+zongyi_vod_pic+'" typeid="'+zongyi_type_id+'" vodid="'+zongyi_vod_id+'" onclick="get_info_jump(this)" alt="图片正在赶来的路上..."' +
					'title="'+zongyi_vod_name+'"class="u3_div_img" ><div class="caption">' +
					'<a onclick="get_info_jump(this)" typeid="'+zongyi_type_id+'" vodid="'+zongyi_vod_id+'" class="u3_div_text" title="' + zongyi_vod_name +'">' + zongyi_vod_name + '</a>' +
					'<p class="u3_div_text_p">' + zongyi_vod_remarks + '</p>' +
					'</div>' + '</div>' + '</div>'

				$("#u3_div_zy").append(div_str);


				var dongman = get_Movs(4,1)
				//影视名称
				dongman_vod_name = dongman[i].vod_name;
				//影视更新集数
				dongman_vod_remarks = dongman[i].vod_remarks;
				//影视数据id
				dongman_vod_id = dongman[i].vod_id;
				//类别id
				dongman_type_id = dongman[i].type_id;
				//类别名称
				dongman_type_name = dongman[i].type_name;
				//获取剧集详细信息
				dongman_infoData = get_info(dongman_vod_id);
				//封面信息
				dongman_vod_pic = dongman_infoData.vod_pic;

				var div_str = '<div class="col-md-3">' +
					'<div class="thumbnail">' +
					'<img src="'+dongman_vod_pic+'" typeid="'+dongman_type_id+'" vodid="'+dongman_vod_id+'" onclick="get_info_jump(this)" alt="图片正在赶来的路上..."' +
					'title="'+dongman_vod_name+'"class="u3_div_img" ><div class="caption">' +
					'<a onclick="get_info_jump(this)" typeid="'+dongman_type_id+'" vodid="'+dongman_vod_id+'" class="u3_div_text" title="' + dongman_vod_name +'">' + dongman_vod_name + '</a>' +
					'<p class="u3_div_text_p">' + dongman_vod_remarks + '</p>' +
					'</div>' + '</div>' + '</div>'

				$("#u3_div_dm").append(div_str);

			}
		},
		error(jqXHR, textStatus, errorThrown) {
			// 错误信息处理
			console.error(textStatus, errorThrown)
		}
	});
}

//获取详细信息
function get_info(vod_id) {
	var infoJson = "";
	$.ajax({
		type: 'GET',
		async: false, //修改此方法为同步，解决ajax是异步请求问题
		url: api_url+'?ac=detail&ids='+vod_id+'&token='+api_token,
		dataType: 'json',
		success(data) {
			infoJson = data.list[0];
		},
		error(jqXHR, textStatus, errorThrown) {
			// 错误信息处理
			console.error(textStatus, errorThrown)
		}
	});
	return infoJson;
}

//获取影视数据
function get_Movs(tid,page) {
	var movs = "";
	$.ajax({
		type: 'GET',
		async: false, //修改此方法为同步，解决ajax是异步请求问题
		url: api_url+'?ac=list&t='+tid+'&page='+page+'&token='+api_token,
		dataType: 'json',
		success(data) {
			// console.log(data.list)
			movs = data.list;
		},
		error(jqXHR, textStatus, errorThrown) {
			// 错误信息处理
			console.error(textStatus, errorThrown)
		}
	});
	return movs;
}

//跳转详情页面
function get_info_jump(elmnt) {
	let vodid = $(elmnt).attr("vodid");
	let title = $(elmnt).attr("title");
	//console.log(cateId + "   " + onlyStr);
	//console.log(window.location.pathname)
	url = "./info.html?vodid=" + vodid + "&title=" + title; //此处拼接内容
	window.location.href = url;
}

//获取当前影视播放地址
function jump_play(elmnt) {
	let mov_url = $(elmnt).attr("value");
	let vodid = $(elmnt).attr("vodid");
	play_url = "./player.html?url=" + mov_url + "&vodid=" + vodid + "&title=" + title;
	window.location.href = play_url;
}


//修改影视分类选中颜色

function type_color(type_name) {

	types = {"动作": "dongzuo", "喜剧": "xiju", "爱情": "aiqing", "科幻": "kehuan", "恐怖": "kongbu", "剧情": "juqing" ,
		"国产": "guochan", "欧美": "oumei", "韩国": "hanju", "日本": "riju", "香港": "gangtai", "海外剧": "haiwai",
		"国产动漫" : "guoman","日本动漫" : "riman","动漫" : "jingdian","欧美动漫" : "oumeidongman","海外动漫" : "haiwaidongman","动画电影" : "donghuadianying",
		"大陆综艺" : "daluzongyi","日韩综艺" : "rihanzongyi","港台综艺" : "gangtaizongyi","欧美综艺" : "oumeizongyi","综艺" : "jingdianzongyi","大陆" : "dalu"}

	$("#"+types[type_name]+"").css("color","#FFFFFF");
	$("#"+types[type_name]+"").css("background-color","#257CFF");
	$("#"+types[type_name]+"").css("-webkit-box-shadow","#257CFF 0px 0px 20px");
	$("#"+types[type_name]+"").css("-moz-box-shadow","#257CFF 0px 0px 20px");
	$("#"+types[type_name]+"").css("box-shadow","#257CFF 0px 0px 20px");

}


function search(vod_name) {


	var movs_search = "";
	$.ajax({
		type: 'GET',
		async: false, //修改此方法为同步，解决ajax是异步请求问题
		url: api_url+'?wd='+vod_name+'&token='+api_token,
		dataType: 'json',
		success(data) {
			// console.log(data.list)
			movs_search = data.list;
			$("#search_num").text(movs_search.length)
		},
		error(jqXHR, textStatus, errorThrown) {
			// 错误信息处理
			console.error(textStatus, errorThrown)
		}
	});

	for(i=0;i<movs_search.length;i++){
		// console.log(movsData[i])
		vname = movs_search[i].vod_name;
		//影视更新集数
		vod_remarks = movs_search[i].vod_remarks;
		//影视数据id
		vod_id = movs_search[i].vod_id;
		//类别id
		type_id = movs_search[i].type_id;
		//类别名称
		type_name = movs_search[i].type_name;
		//获取剧集详细信息
		infoData = get_info(vod_id);
		//封面信息
		vod_pic = infoData.vod_pic;

		var div_str = '<div class="col-md-6"><div class="thumbnail" id="search_div"><div class="row">' +
			'									<div class="col-md-4"><img id="search_div_img" src="' + vod_pic + '" alt="图片正在赶来的路上">' +
			'									</div><div class="col-md-8"><div><p><span id="info_name" name="名称">' + vname + '</span></p><p>' +
			'												<span id="info_cateName" name="类型">类型：</span>' +
			'												<span class="u3s_info_cateName_text">' + type_name + '</span>' +
			'											</p><p><span id="info_years" name="年份">年份：</span>' +
			'												<span class="u3s_info_years_text">' + infoData.vod_year + '</span>' +
			'											</p><p>' +
			'												<span id="info_area" name="地区">地区：</span>' +
			'												<span class="u3s_info_area_text">' + infoData.vod_area + '</span>' +
			'											</p><div class="layui-btn-container" id="info_play" title="'+vname+'" vodid="'+vod_id+'"onclick="get_info_jump(this)">' +
			'												<button type="button" id="info_play_btn"' +
			'													class="layui-btn layui-btn-radius layui-btn-primary">' +
			'													<i class="layui-icon layui-icon-play site-doc-icon"></i>' +
			'													<span>查看详情</span></button></div></div></div></div></div></div>';


		$("#search").append(div_str);

	}
}
